<?php

use Biz\model\wiki\ObjRelateWiki as mObjRelateWiki;
use Biz\model\wiki\Wiki as mWiki;
use Biz\service\wiki\Wiki;
use Doraemon\amqp\CommunityQueue;
use Doraemon\module\ContentChecking\Infrastructure\Api\Themis;
use Doraemon\pockets\constInterface\statSign\AutoModule;
use Doraemon\service\CommunityMcn\AddPoints;
use Doraemon\service\Creators\AddWikiPoints;
use Doraemon\tools\ActionLog;
use Doraemon\tools\Creator\GrowthConfig as tGrowthConfig;
use Doraemon\tools\IdParse;
use Doraemon\tools\Log;
use Doraemon\tools\PointConfig;
use Doraemon\cache\Mcn\McnClickCounter;
use \Doraemon\pockets\IdSender\IdSender;



class ClickBuyAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_NOTHING;

    public function main()
    {
        $uid = $this->uid;
        $wikiId = $this->getRequest()->getPost('wiki_id', 0);
        $actFrom = $this->getRequest()->getPost('act_from', '');
        $statSign = $this->getRequest()->getPost('statSign', '');
        $wikiInfos = $wikiId ? Wiki::getWikiBaseInfos([$wikiId]) : [];
        $wikiInfo = $wikiInfos[$wikiId] ?? [];
        if ($uid && !empty($wikiInfo) && $wikiInfo['channel'] != mWiki::CHANNEL_NOTHING) {
            //积分操作-商业-购买增加积分
            \Doraemon\amqp\CommunityQueue::insertQueue(PointConfig::MQ_QUEUE_URL, [
                'event_name' => PointConfig::POINT_TYPE_BUSINESS_BUY,
                'obj_id' => $wikiId,
                'uid' => $uid,
                'type' => PointConfig::POINT_AWARD
            ], \Doraemon\amqp\CommunityQueue::QUEUE_COMMUNITY);
        }
        //mcn 2.0
        if ($statSign && !empty($wikiInfo) && $wikiInfo['channel'] == mWiki::CHANNEL_TAOBAO) {
            $statSignArr = json_decode($statSign, true);
            Log::debug('statSign', $statSignArr);
            $objId = $statSignArr ? $this->getObjId($statSignArr) : '';
            if (IdSender::getType($objId) != IdSender::TYPE_WIKI_ALBUM) {
                $relateList = $objId ? mObjRelateWiki::getByWhere([
                    'obj_id' => $objId,
                    'wiki_id' => $wikiId,
                    'status' => mObjRelateWiki::STATUS_NORMAL,
                ], ['obj_id']) : [];
            } else {
                $relateList = $objId;
            }
            if ($relateList) {
//                try {
                    //创作者中心3.0--点击wiki增加成长值
                    $params = [
                        'event_name' => tGrowthConfig::WIKI_GROWTH_CLICK,
                        'obj_id' => $objId,
                        'uid' => IdParse::getUid($objId),
                        'action_uid' => $uid,
                        'remarks' => json_encode(['other_id' => $wikiId], JSON_UNESCAPED_UNICODE),
                    ];
                    CommunityQueue::insertQueue(tGrowthConfig::MQ_QUEUE_URL, $params, CommunityQueue::QUEUE_COMMUNITY);
//
//                    if(IdSender::getType($objId) == IdSender::TYPE_PHOTO || IdSender::getType($objId) == IdSender::TYPE_ANSWER){
//                        McnClickCounter::addPhtotoClickCounter($objId);
//                    }
//                    //加积分
//                    $mcnPoint = new AddPoints($uid, $objId, $wikiId);
//                    $mcnPoint->add();
//                } catch (\Exception $e) {
//                    Log::debug('加积分失败', $e->getMessage());
//                }
                try {
                    //加积分
                    $wikiPoint = new AddWikiPoints($uid, $objId, $wikiId);
                    $wikiPoint->add();
                } catch (\Exception $e) {
                    Log::debug('click_buy_wiki', [$uid, $objId, $wikiId, $e->getMessage(), $e->getTrace(), $e->getLine(), $e->getFile()]);
                }
                try{
                    //反垃圾记录和检测。
                    Themis::count4BusinessUserClickWiki($this->uid,IdParse::getUidFromObjId($objId));
                    Themis::check4BusinessUserClickWiki($this->uid,IdParse::getUidFromObjId($objId));
                }catch (\Throwable $e){
                    Log::debug('Themis fail', [$this->uid,$objId,$e->getMessage(),$e->getTraceAsString()]);
                }
            }

        }
        ActionLog::log($this->uid, ['wiki_id' => $wikiId, 'act_from' => $actFrom, 'statSign' => $statSign], ActionLog::BIZ_WIKI_CLICK_BUY);
        $this->setSuccess([]);
    }

    private function getObjId($statSignArr)
    {
        if(!is_array($statSignArr)){
            $statSignArr = json_decode($statSignArr,true);
        }
        $params = $statSignArr['ps'] ?? [];
        $objId = $params['obj_id'] ?? '';
        if (!$objId) {
            $objId = $params['relate_id'] ?? '';
        }
        if (isset($statSignArr['m']) && $statSignArr['m'] == AutoModule::ALBUM_DETAIL_PAGE_WIKI_CARD) {
            $objId = $statSignArr['roi'];
        }
        return $objId;
    }
}